package dao.daoimpl;

import util.DataBase;
import dao.CommentDao;
import dao.LabelDao;
import dao.SearchDao;
import bean.Article;
import bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class SearchDaoImpl implements SearchDao {
    @Override
    public ArrayList<Article> labelSearch(String content) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        String sql="select article_table.* from article_table,label_table,article_label where article_table.article_id=article_label.article_id and label_table.label_id=article_label.label_id and label_table.label_content like '%"+content+"%'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()) {
            String contents = resultSet.getString("article_table.article_content");
            int likingDegree = resultSet.getInt("article_table.article_likingDegree");
            int views = resultSet.getInt("article_table.article_views");
            String title = resultSet.getString("article_table.article_title");
            String time1 = resultSet.getString("article_table.article_time");
            int commentCount = resultSet.getInt("article_table.article_commentCount");
            int category = resultSet.getInt("article_table.article_categoryId");
            String userAccount = resultSet.getString("article_table.article_user");
            int id = resultSet.getInt("article_table.article_id");
            String type1 = resultSet.getString("article_table.article_type");
            String subfield1 = resultSet.getString("article_table.article_subfield");
            int approve = resultSet.getInt("article_table.article_approve");
            int like = resultSet.getInt("article_table.article_id");
            articles.add(new Article(id, contents, likingDegree, views, title, time1, commentCount, category, userAccount, labelDao.getLabels(id), commentDao.getComments(id), type1, subfield1, approve, like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public ArrayList<User> userSearch(String content) throws SQLException {
        ArrayList<User> users=new ArrayList<>();
        String sql="select * from user_table where user_account like '%"+content+"%' or user_screenName like '%"+content+"%' or user_sign like '%"+content+"%' or user_name like '"+content+"' and user_isAdmin = 0";
        DataBase.init();
        ResultSet resultSet= DataBase.inquire(sql);
        while(resultSet.next()){
                String account=resultSet.getString("user_account");
                String psw=resultSet.getString("user_password");
                String address=resultSet.getString("user_address");
                String birthday=resultSet.getString("user_birthday");
                String name=resultSet.getString("user_name");
                String screenName=resultSet.getString("user_screenName");
                String email=resultSet.getString("user_email");
                String headPortrait=resultSet.getString("user_headPortrait");
                String sign=resultSet.getString("user_sign");
                String sex=resultSet.getString("user_sex");
                String phone=resultSet.getString("user_phone");
                int age=resultSet.getInt("user_age");
                int isAdmin=resultSet.getInt("user_isAdmin");
                users.add(new User(account,psw,name,screenName,sign,age,sex,phone,address,birthday,email,isAdmin,headPortrait));
            }
        DataBase.release();
        return users;
    }

    @Override
    public ArrayList<Article> contentSearch(String content) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        String sql="select *  from article_table where article_content like '%"+content+"%' and article_approve= 1";
        DataBase.init();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            String contents = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_id");
            articles.add(new Article(id,contents,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public ArrayList<Article> titleSearch(String content) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        String sql="select *  from article_table where article_title like '%"+content+"%' and article_approve= 1";
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            String contents = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_id");
            articles.add(new Article(id,contents,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public ArrayList<Article> moldSearch(String content) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        DataBase.init();
        String sql="select *  from article_table where article_type like '%"+content+"%' and article_approve= 1";
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            String contents = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_id");
            articles.add(new Article(id,contents,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public ArrayList<Article> subfieldSearch(String content) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        DataBase.init();
        String sql="select *  from article_table where article_subfield like '%"+content+"%' and article_approve= 1";
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            String contents = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_id");
            articles.add(new Article(id,contents,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public ArrayList<Article> synthesisSearch(String content) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        DataBase.init();
        String sql="select *  from article_table where article_subfield like '%"+content+"%' or article_content like '%"+content+"%' or article_type like '%"+content+"%' or article_title like '"+content+"' and article_approve= 1";
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            String contents = resultSet.getString("article_content");
            int likingDegree = resultSet.getInt("article_likingDegree");
            int views = resultSet.getInt("article_views");
            String title = resultSet.getString("article_title");
            String time = resultSet.getString("article_time");
            int commentCount = resultSet.getInt("article_commentCount");
            int category = resultSet.getInt("article_categoryId");
            String userAccount = resultSet.getString("article_user");
            int id = resultSet.getInt("article_id");
            String type = resultSet.getString("article_type");
            String subfield=resultSet.getString("article_subfield");
            int approve=resultSet.getInt("article_approve");
            int like=resultSet.getInt("article_id");
            articles.add(new Article(id,contents,likingDegree,views,title,time,commentCount,category,userAccount,labelDao.getLabels(id),commentDao.getComments(id),type,subfield,approve,like));
        }
        DataBase.release();
        return articles;
    }

    @Override
    public ArrayList<Article> getInquireArticles(String time, String subfield, String label, String type, User user) throws SQLException {
        ArrayList<Article> articles=new ArrayList<>();
        LabelDao labelDao=new LabelDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        DataBase.init();
        String sql = "select distinct article_table.* from article_table,article_label,label_table where article_table.article_id=article_label.article_Id and label_table.label_id=article_label.label_id and article_table.article_user='" + user.getAccount() + "'and article_table.article_approve=1";
        if(!time.equals("")){
            sql=sql+" and article_table.article_time like '%"+time+"%'";
            if(!subfield.equals("不限")){
                sql=sql+" and article_table.article_subfield='"+subfield+"'";
                if(!type.equals("不限")) {
                    sql=sql+ " and article_table.article_type='" + type + "'";
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }else{
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }
            }else{
                if(!type.equals("不限")) {
                    sql=sql+ " and article_table.article_type='" + type + "'";
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }else{
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }
            }
        }else{
            if(!subfield.equals("不限")){
                sql=sql+" and article_table.article_subfield='"+subfield+"'";
                if(!type.equals("不限")) {
                    sql=sql+ " and article_table.article_type='" + type + "'";
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }else{
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }
            }else{
                if(!type.equals("不限")) {
                    sql=sql+ " and article_table.article_type='" + type + "'";
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }else{
                    if(!label.equals("不限")){
                        sql=sql+" and label_table.label_content='"+label+"'";
                    }
                }
            }
        }
        System.out.println(sql);
        ResultSet resultSet = DataBase.inquire(sql);
        while (resultSet.next()) {
            String content = resultSet.getString("article_table.article_content");
            int likingDegree = resultSet.getInt("article_table.article_likingDegree");
            int views = resultSet.getInt("article_table.article_views");
            String title = resultSet.getString("article_table.article_title");
            String time1 = resultSet.getString("article_table.article_time");
            int commentCount = resultSet.getInt("article_table.article_commentCount");
            int category = resultSet.getInt("article_table.article_categoryId");
            String userAccount = resultSet.getString("article_table.article_user");
            int id = resultSet.getInt("article_table.article_id");
            String type1 = resultSet.getString("article_table.article_type");
            String subfield1 = resultSet.getString("article_table.article_subfield");
            int approve = resultSet.getInt("article_table.article_approve");
            int like = resultSet.getInt("article_table.article_id");
            articles.add(new Article(id, content, likingDegree, views, title, time1, commentCount, category, userAccount, labelDao.getLabels(id), commentDao.getComments(id), type1, subfield1, approve, like));
        }
        DataBase.release();
        return articles;
    }
}
